package com.itany.nmms.controller;

import com.itany.mvc.annotation.RequestMapping;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.StaffService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/27 15:30
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/staff")
public class StaffController {

    private StaffService staffService = (StaffService) ObjectFactory.getObject("staffService");

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String loginName = request.getParameter("loginName");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String code= request.getParameter("code");
        String image = (String) request.getSession().getAttribute("code");

        try {
            Staff staff = staffService.login(loginName,password,role,code,image);
            // 方法的返回值用于控制页面跳转
            // 共有三种格式
            // 1.redirect:url  --  重定向到指定的url地址
            // 2.url  --  转发到指定的url地址
            // 3.视图名 -- 跳转到指定的视图
                // 此时视图会进入到控制器中进行处理
                // 即:会自动添加前缀与后缀
            return "redirect:/showMain.do";
        } catch (RequestParameterErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            return "/showLogin.do";
        } catch (CodeErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            return "backend/login";
        } catch (StaffNotExistException e) {
            request.setAttribute("loginMsg",e.getMessage());
            return "backend/login";
        } catch (ServiceException e) {
            request.setAttribute("loginMsg",e.getMessage());
            return "backend/login";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginMsg","服务器内部异常");
            return "backend/login";
        }
    }

}
